using System;
using Framework.Library.Core.Utils.GlobalHelper;
using Framework.Library.Base.Helper;
using System.Web.UI;
using System.Web.UI.WebControls;
using Framework.Library.Core.Config;

namespace Framework.Library.Base.UI
{
    [ToolboxData("<{0}:FilePicker runat=server></{0}:FilePicker>")]
    public class FilePicker : TextBox, INamingContainer
    {

        private System.Web.UI.WebControls.HyperLink _HyperLink = null;
        private string ImageURL { get; set; }
        public string PopupTitle { get; set; }
        public int ImageLeftOffset = 1;
        public int ImageTopOffset = 2;
        public string Editor { get; set; }
        public string FileType { get; set; }
        public string Source { get; set; }
        public string PopupWidth { get; set; }
        public string PopupHeight { get; set; }
        public string PopupFilter { get; set; }

        protected override void CreateChildControls()
        {
            this._HyperLink = new System.Web.UI.WebControls.HyperLink();
            this._HyperLink.EnableViewState = false;
            this.Controls.Add(this._HyperLink);
            string clientID = this._HyperLink.ClientID;
            this._HyperLink.Attributes["ID"] = clientID;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if(string.IsNullOrEmpty(FileType))
                FileType = "folder";
            ImageURL = "/Resource/Images/filemanager/16/" + FileType + ".png";
            if (string.IsNullOrEmpty(Editor))
                Editor = "standalone";
            if (string.IsNullOrEmpty(Source))
                Source = "text";
            if (string.IsNullOrEmpty(PopupWidth))
                PopupWidth = "1000";
            if (string.IsNullOrEmpty(PopupHeight))
                PopupHeight = "650";
            if (string.IsNullOrEmpty(PopupFilter))
                PopupFilter = "all";
            this._HyperLink.Visible = this.Visible;
            if (this._HyperLink.Visible)
            {
                System.Web.UI.WebControls.Image child = new System.Web.UI.WebControls.Image();
                child.ImageUrl = this.ImageURL;
                child.ToolTip = this.PopupTitle;
                child.Style["POSITION"] = "relative";
                child.Style["TOP"] = this.ImageTopOffset.ToString() + "px";
                child.Style["LEFT"] = this.ImageLeftOffset.ToString() + "px";
                this._HyperLink.Enabled = this.Enabled;
                child.Enabled = this.Enabled;
                this._HyperLink.Controls.Add(child);
                this._HyperLink.NavigateUrl = "javascript:;";
                this._HyperLink.Attributes.Add("onclick", "window.open('/Popup.aspx?p=" + Configurations.PORTALID + "&l=" + Configurations.LANGID + "&c=1102&editor=" + Editor + "&filter=" + PopupFilter + "&source=" + Source + "&returnID=" + this.ClientID + "', 'pdwfilebrowser', 'width=" + PopupWidth + ",height=" + PopupHeight + ",scrollbars=no,toolbar=no,location=no');");
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            this.RenderChildren(writer);
        }
    }
}